package com.lstimeline.statuschecker;

import org.htmlparser.*;

import java.io.UnsupportedEncodingException;
import java.net.*;

import com.lstimeline.utility.CookieManager;

/**
 * The abstract class represents a status checker.
 * @author Harry Zhou
 * @version Jan. 2009
 */
public abstract class AbstractStatusChecker {
	protected HttpURLConnection conn;
	protected Parser parser;
	//Its own connection manager
	protected CookieManager cookieManager;
	// 0: uninitiated
	// 10: success
	// 21: authentication failed
	// 22: other error
	protected int statusCode = 0;
	
	public static final int SUCCESS = 10;
	public static final int AUTH_FAIL = 21;
	public static final int ERR = 22;
	
	/**
	 * The default constructor
	 */
	public AbstractStatusChecker() {
		//each status checker has its own cookieManager
		cookieManager = new CookieManager();
	}
	
	/**
	 * Return whether the retrieval is successful or not
	 * @return the status code
	 */
	public int getStatusCode() {
		return statusCode;
	}
	
	/**
	 * Retrieve the status message
	 * @param username the username
	 * @param password the password
	 * @return the status message
	 */
	public abstract String getStatusMessage(String username, String password);
	
	/**
	 * A helper method that parsers cookies
	 */
	protected void receiveCookies() {
		try {
			conn.connect();
			cookieManager.storeCookies(conn);
			conn.getInputStream().close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * A helper method that setups a HEAD connection
	 * 
	 */
	protected void setupHEADConnection(String urlString) {
		try {
			URL url = new URL(urlString);
	        conn = (HttpURLConnection) url.openConnection();
	        conn.setUseCaches(false);
	        
	        conn.setRequestMethod ("HEAD");
	        
	        conn.setInstanceFollowRedirects(false);
	        
	        conn.setRequestProperty("User-Agent", "LSTimeline.java/alpha");
	        conn.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
	        conn.setRequestProperty("Accept-Language", "en-us,en;q=0.5");
	        conn.setRequestProperty("Accept-Encoding", "gzip,deflate");
	        conn.setRequestProperty("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
	        conn.setRequestProperty("Connection", "Close");
	        
	        cookieManager.setCookies(conn);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * A helper method that setups a GET connection
	 * 
	 */
	protected void setupGETConnection(String urlString) {
		try {
			URL url = new URL(urlString);
	        conn = (HttpURLConnection) url.openConnection();
	        conn.setUseCaches(false);
	        
	        conn.setRequestMethod ("GET");
	        
	        conn.setInstanceFollowRedirects(false);
	        
	        conn.setRequestProperty("User-Agent", "LSTimeline.java/alpha");
	        conn.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
	        conn.setRequestProperty("Accept-Language", "en-us,en;q=0.5");
	        conn.setRequestProperty("Accept-Encoding", "gzip,deflate");
	        conn.setRequestProperty("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
	        conn.setRequestProperty("Connection", "close");
	        
	        cookieManager.setCookies(conn);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * A helper method that setups a post connection
	 */
	protected void setupPOSTConnection(String urlString, int contentLength) {
		try {
			URL url = new URL(urlString);
	        conn = (HttpURLConnection) url.openConnection();
	        conn.setUseCaches(false);
	        
	        conn.setRequestMethod ("POST");
	        conn.setInstanceFollowRedirects(false);
	        conn.setDoOutput(true);
	        
	        conn.setRequestProperty("User-Agent", "LSTimeline.java/alpha");
	        conn.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
	        conn.setRequestProperty("Accept-Language", "en-us,en;q=0.5");
	        conn.setRequestProperty("Accept-Encoding", "gzip,deflate");
	        conn.setRequestProperty("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
	        conn.setRequestProperty("Connection", "close");
	        conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
	        conn.setRequestProperty("Content-Length", String.valueOf(contentLength));
	        
	        cookieManager.setCookies(conn);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * A helper method that sets up query paramester string
	 */
	protected void queryBuilder(StringBuilder builder, String paramName, String paramValue) throws UnsupportedEncodingException {
			builder.append(URLEncoder.encode(paramName, "UTF-8"));
			builder.append("=");
			builder.append(URLEncoder.encode(paramValue, "UTF-8"));
			builder.append("&");
	}
}
